<?php
class NICED_DBResult 
{
	const ASSOC = 1;
	const NUM   = 2;
	const BOTH  = 3;
	const OBJECT= 4;
	const FETCH_CLASS = 5;

	protected $db;
	protected $rst;
	protected $sql;
	protected $errorMessage;
	protected $errorCode;
	protected $rowCallbacks = array();

	public function __construct($db, $rst, $sql) 
	{
		$this->db  = $db;
		$this->rst = $rst;
		$this->sql = $sql;
	}

	public function fetchRow($type = null) 
	{
		$row = array();

		//call user callback
		for($i = 0; $i < sizeof($this->rowCallbacks); $i++) 
		{
			$row = call_user_func($this->rowCallbacks[$i], $row);
		}

		return $row;
	}

	public function insertId() 
	{
		return false;
	}

	public function free() 
	{
		return false;
	}

	public function isSuccess() 
	{
		return false;
	}

	public function fetchAll($type = null) 
	{
		$rows = array();

		while ($row = $this->fetchRow($type)) 
		{
			$rows[] = $row;
		}	

		return $rows;		
	}

    public function addRowFormatter($cb)
    {
        $this->rowCallbacks[] = $cb;
    
    }

	public function getRst()
	{
		return $this->rst;
	}

	public function getErrorMessage() 
	{
		return $this->errorMessage;
	}

	public function getErrorCode() 
	{
		return $this->errorCode;
	}

	public function getDB()
	{
		return $this->db;
	}

    public function getSQL()
    {
        return $this->sql;
    }

    public function freeResult()
    {
        unset($this->rst);
    }
}//end class
